package com.xdtech.hotel.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.hotel.service.HotelInfoService;
import com.xdtech.hotel.vo.HotelInfoItem;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2015-04-04 11:00:12
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class HotelInfoController {
	@Autowired
	private HotelInfoService hotelInfoService;
	@RequestMapping(value="/hotelInfo.do",params = "hotelInfo")
	public ModelAndView skipHotelInfo(HttpServletRequest request) {
		request.setAttribute("hotelInfoItem", hotelInfoService.loadOneHotelInfoItem());
		return new ModelAndView("hotel/hotelInfo/hotelInfo_ftl");
	}
	
	@RequestMapping(value="/hotelInfo.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(HttpServletRequest request,Pagination pg) {
		Map<String, Object> results = null;
		if (StringUtils.isEmpty(request.getParameter("page"))) {
			//不分页处理
			results = hotelInfoService.loadPageAndCondition(null, null);
		}else {
			results = hotelInfoService.loadPageAndCondition(pg, null);
		}
		
		return results;
	}
	
	@RequestMapping(value="/hotelInfo.do",params = "editHotelInfo")
	public ModelAndView editHotelInfo(HttpServletRequest request,Long hotelInfoId) {
		if (hotelInfoId!=null) {
			request.setAttribute("hotelInfoItem", hotelInfoService.loadHotelInfoItem(hotelInfoId));
		}
		return new ModelAndView("hotel/hotelInfo/editHotelInfo_ftl");
	}
	
	@RequestMapping(value="/hotelInfo.do",params = "saveHotelInfo")
	@ResponseBody
	public ResultMessage saveHotelInfo(HotelInfoItem item) {
		ResultMessage r = new ResultMessage();
		if (hotelInfoService.saveOrUpdateHotelInfo(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/hotelInfo.do",params = "deleteHotelInfoItems")
	@ResponseBody
	public ResultMessage deleteHotelInfoItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> hotelInfoIds = new ArrayList<Long>();
			for (String id : tempIds) {
				hotelInfoIds.add(Long.valueOf(id));
			}
			hotelInfoService.deleteHotelInfoInfo(hotelInfoIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}

}
